
cd "${data}"
use"dataset.dta", clear 

*Create a new folder under data that is called "pvalues"

cd "${data}/pvalues"

drop if missing(selfplacement) | missing(voted)
keep if samplelimit==1
	
set more off
tempname turnout_file

postfile `turnout_file' sample b_voted se_voted p_voted str60 sample_lbl using selfplacement.dta, replace 

levelsof country_year_, local(levels)
local vlbl : value label country_year_

foreach s of local levels {
    local t : label `vlbl' `s'

    quietly regress selfplacement voted if country_year_==`s', robust
    scalar p_value = 2 * ttail(e(df_r), abs(_b[voted]/_se[voted]))

    post `turnout_file' (`s') (_b[voted]) (_se[voted]) (p_value) ("`t'")
}

postclose `turnout_file'


use selfplacement.dta, clear

histogram p_voted, bin(57) frequency fcolor(gs8%50) lcolor(gs4%50) lpattern(blank) xline(0.05, lwidth(medthick) lpattern(dash) lcolor(black))  xtitle("") title("left-right") ytitle("")

graph save "pvalues_lr.gph", replace 

*graph hbox p_voted, yline(0.05, lwidth(medium) lpattern(dash) lcolor(black)) box(1,fcolor(gs1)) ytitle("p values") title("left-right")
*graph save "pvalues_lr2.gph", replace 

cd "${data}"
use "dataset.dta", clear 
cd "${data}/pvalues"

drop if missing(redistribution) | missing(voted)
keep if samplelimit==1

		
set more off
tempname turnout_file

postfile `turnout_file' sample b_voted se_voted p_voted str60 sample_lbl using redistribution.dta, replace 

levelsof country_year_, local(levels)
local vlbl : value label country_year_

foreach s of local levels {
    local t : label `vlbl' `s'

    quietly regress redistribution voted if country_year_==`s', robust
    scalar p_value = 2 * ttail(e(df_r), abs(_b[voted]/_se[voted]))

    post `turnout_file' (`s') (_b[voted]) (_se[voted]) (p_value) ("`t'")
}
postclose `turnout_file'

use redistribution.dta, clear


histogram p_voted, bin(57) frequency fcolor(gs8%50) lcolor(gs4%50) lpattern(blank) xline(0.05, lwidth(medthick) lpattern(dash) lcolor(black)) xtitle("") title("redistribution") ytitle("")

graph save "pvalues_redistribution.gph", replace 

*graph hbox p_voted, yline(0.05, lwidth(medium) lpattern(dash) lcolor(black)) box(1,fcolor(gs1)) ytitle("") title("redistribution")
*graph save "pvalues_redistribution2.gph", replace 

cd "${data}"
use"dataset.dta", clear 
cd "${data}/pvalues"


drop if missing(regulation) | missing(voted)
keep if samplelimit==1

set more off
tempname turnout_file

postfile `turnout_file' sample b_voted se_voted p_voted str60 sample_lbl  using regulation.dta, replace 

levelsof country_year_, local(levels)
local vlbl : value label country_year_

foreach s of local levels {
    local t : label `vlbl' `s'

    quietly regress regulation voted if country_year_==`s', robust
    scalar p_value = 2 * ttail(e(df_r), abs(_b[voted]/_se[voted]))

    post `turnout_file' (`s') (_b[voted]) (_se[voted]) (p_value) ("`t'")
}

postclose `turnout_file'


use regulation.dta, clear
	

histogram p_voted, bin(57) frequency fcolor(gs8%50) lcolor(gs4%50) lpattern(blank) xline(0.05, lwidth(medthick) lpattern(dash) lcolor(black)) xtitle("") title("regulation") ytitle("")

graph save "pvalues_regulation.gph", replace 
	
cd "${data}"
use"dataset.dta", clear 
cd "${data}/pvalues"


drop if missing(spending) | missing(voted)
keep if samplelimit==1

		
set more off
tempname turnout_file

postfile `turnout_file' sample b_voted se_voted p_voted str60 sample_lbl  using spending.dta, replace 

levelsof country_year_, local(levels)
local vlbl : value label country_year_


foreach s of local levels {
    local t : label `vlbl' `s'

    quietly regress spending voted if country_year_==`s', robust
    scalar p_value = 2 * ttail(e(df_r), abs(_b[voted]/_se[voted]))

    post `turnout_file' (`s') (_b[voted]) (_se[voted]) (p_value) ("`t'")
}

postclose `turnout_file'

use spending.dta, clear	

histogram p_voted, bin(57) frequency fcolor(gs8%50) lcolor(gs4%50) lpattern(blank) xline(0.05, lwidth(medthick) lpattern(dash) lcolor(black)) xtitle("") title("spending") ytitle("")

graph save "pvalues_spending.gph", replace 
	
************

	
cd "${data}"
use"dataset.dta", clear 
cd "${data}/pvalues"


drop if missing(environment) | missing(voted)
keep if samplelimit==1

set more off
tempname turnout_file

postfile `turnout_file' sample b_voted se_voted p_voted str60 sample_lbl using environment.dta, replace 

levelsof country_year_, local(levels)
local vlbl : value label country_year_


foreach s of local levels {
    local t : label `vlbl' `s'

    quietly regress environment voted if country_year_==`s', robust
    scalar p_value = 2 * ttail(e(df_r), abs(_b[voted]/_se[voted]))

    post `turnout_file' (`s') (_b[voted]) (_se[voted]) (p_value) ("`t'")
}

postclose `turnout_file'

use environment.dta, clear	
	

	histogram p_voted, bin(57) frequency fcolor(gs8%50) lcolor(gs4%50) lpattern(blank) xline(0.05, lwidth(medthick) lpattern(dash) lcolor(black)) xtitle("") title("environment") ytitle("")

graph save "pvalues_environment.gph", replace 


cd "${data}"
use"dataset.dta", clear 
cd "${data}/pvalues"



drop if missing(immigration) | missing(voted)
keep if samplelimit==1

		
set more off
tempname turnout_file

postfile `turnout_file' sample b_voted se_voted p_voted str60 sample_lbl  using immigration.dta, replace 

levelsof country_year_, local(levels)
local vlbl : value label country_year_



foreach s of local levels {
    local t : label `vlbl' `s'

    quietly regress immigration voted if country_year_==`s', robust
    scalar p_value = 2 * ttail(e(df_r), abs(_b[voted]/_se[voted]))

    post `turnout_file' (`s') (_b[voted]) (_se[voted]) (p_value) ("`t'")
}

postclose `turnout_file'

use immigration.dta, clear	
	

	histogram p_voted, bin(57) frequency fcolor(gs8%50) lcolor(gs4%50) lpattern(blank) xline(0.05, lwidth(medthick) lpattern(dash) lcolor(black)) xtitle("") title("immigration") ytitle("")

graph save "pvalues_immigration.gph", replace 
	

	
cd "${data}"
use"dataset.dta", clear 
cd "${data}/pvalues"




drop if missing(samesexmarriage) | missing(voted)
keep if samplelimit==1

		
set more off
tempname turnout_file

postfile `turnout_file' sample b_voted se_voted p_voted str60 sample_lbl using samesexmarriage.dta, replace 

levelsof country_year_, local(levels)
local vlbl : value label country_year_


foreach s of local levels {
    local t : label `vlbl' `s'

    quietly regress samesexmarriage voted if country_year_==`s', robust
    scalar p_value = 2 * ttail(e(df_r), abs(_b[voted]/_se[voted]))

    post `turnout_file' (`s') (_b[voted]) (_se[voted]) (p_value) ("`t'")
}

postclose `turnout_file'

use samesexmarriage.dta, clear	
	

	histogram p_voted, bin(57) frequency fcolor(gs8%50) lcolor(gs4%50) lpattern(blank) xline(0.05, lwidth(medthick) lpattern(dash) lcolor(black)) xtitle("") title("same-sex marriage") ytitle("")

graph save "pvalues_samesex.gph", replace 
			

			
cd "${data}"
use"dataset.dta", clear 
cd "${data}/pvalues"



drop if missing(laworder) | missing(voted)
keep if samplelimit==1
		
set more off
tempname turnout_file

postfile `turnout_file' sample b_voted se_voted p_voted str60 sample_lbl using laworder.dta, replace 

levelsof country_year_, local(levels)
local vlbl : value label country_year_


foreach s of local levels {
    local t : label `vlbl' `s'

    quietly regress laworder voted if country_year_==`s', robust
    scalar p_value = 2 * ttail(e(df_r), abs(_b[voted]/_se[voted]))

    post `turnout_file' (`s') (_b[voted]) (_se[voted]) (p_value) ("`t'")
}


postclose `turnout_file'

use laworder.dta, clear	
	


	histogram p_voted, bin(57) frequency fcolor(gs8%50) lcolor(gs4%50) lpattern(blank) xline(0.05, lwidth(medthick) lpattern(dash) lcolor(black)) xtitle("") title("civil liberties") ytitle("")

graph save "pvalues_laworder.gph", replace 
				

cd "${data}"
use"dataset.dta", clear 
cd "${data}/pvalues"



drop if missing(euintegration) | missing(voted)
keep if samplelimit==1

		
set more off
tempname turnout_file

postfile `turnout_file' sample b_voted se_voted p_voted str60 sample_lbl using euintegration.dta, replace 

levelsof country_year_, local(levels)
local vlbl : value label country_year_

foreach s of local levels {
    local t : label `vlbl' `s'

    quietly regress euintegration voted if country_year_==`s', robust
    scalar p_value = 2 * ttail(e(df_r), abs(_b[voted]/_se[voted]))

    post `turnout_file' (`s') (_b[voted]) (_se[voted]) (p_value) ("`t'")
}

postclose `turnout_file'

use euintegration.dta, clear	

	
histogram p_voted, bin(57) frequency fcolor(gs8%50) lcolor(gs4%50) lpattern(blank) xline(0.05, lwidth(medthick) lpattern(dash) lcolor(black)) xtitle("") title("EU integration") ytitle("")

graph save "pvalues_euintegration.gph", replace 

cd "${data}/pvalues"
graph combine pvalues_lr.gph pvalues_redistribution.gph pvalues_regulation.gph pvalues_spending.gph pvalues_environment.gph pvalues_immigration.gph pvalues_samesex.gph pvalues_laworder.gph pvalues_euintegration.gph, ycommon 
	graph save "${figures}/Figure2.gph", replace 
	graph export "${figures}/Figure2.png", as(png) replace 

	*also save the appended pvalues for later use. 
	
cd "${data}/pvalues"

use "selfplacement.dta", clear  
gen item = 1
save "selfplacement.dta", replace  

use "redistribution.dta", clear  
gen item = 2
save "redistribution.dta", replace  

use "regulation.dta", clear  
gen item = 3
save "regulation.dta", replace 

use "spending.dta", clear  
gen item = 4
save "spending.dta", replace 

use "environment.dta", clear  
gen item = 5
save "environment.dta", replace 

use "immigration.dta", clear  
gen item = 6
save "immigration.dta", replace 

use "samesexmarriage.dta", clear  
gen item = 7
save "samesexmarriage.dta", replace 

use "laworder.dta", clear  
gen item = 8
save "laworder.dta", replace 

use "euintegration.dta", clear  
gen item = 9
save "euintegration.dta", replace 



use "selfplacement.dta", clear 
append using "redistribution.dta" 
append using "regulation.dta" 
append using "spending.dta" 
append using "environment.dta" 
append using "immigration.dta" 
append using "samesexmarriage.dta" 
append using "laworder.dta" 
append using "euintegration.dta"


 label define itemlabels 1"left-right" 2"redistribution" 3"regulation" 4"spending" 5"environment" 6"immigration" 7"same-sex marriage" 8"civil liberties" 9"EU integration"
 label values item itemlabels 
 
   
  egen id = group(sample item)
  isid id 
  duplicates report id // there should be no duplicates
 
save "pvalues_merged.dta", replace 





